export const BASE_URL = 'https://hw.37cpep.com';
export const IMG_URL = 'https://hw.37cpep.com/uploads/';
export const OSS_URL = 'https://shangqi-huiwu.oss-cn-beijing.aliyuncs.com';
 // export const BASE_URL = 'http://meet.oss';
 // export const IMG_URL = 'http://meet.cn:8011/uploads/';

export const USER_ID = getApp().globalData.userid;
export const USER_INFO = getApp().globalData.userinfo;
// import func from '../common/func.js'; 

export const myRequest = (url, method, data = {}, header = {}) => {
	//func.loading('正在加载中...')
	return new Promise((resolve, reject) => {
		var userinfo=uni.getStorageSync('user_info')
		var userid=userinfo['id']
		console.log(BASE_URL + url)
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				 // 'Content-Type': 'application/json',
				'Authorization': userid
			} || header,
			data: data || {},
			success: (res) => {
				console.log(res)
				uni.hideLoading();
				let code = res.data.code;
				//console.log(res.data)
				if (code == 0) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
			
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '请求接口失败',
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}
/**
 *  count-- 可选张数
 * 	arr-- 最终结果   [{img1:'全路径',img2:'半路径'},{img1:'全路径',img2:'半路径'}]
 */
export let upload = (count = 1) => {
	console.log(count);
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count,
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			success: function(res) {
				uni.showLoading({
					title: '上传中'
				})
				let imgarr = res.tempFilePaths
				let arr = []
				imgarr.forEach(item => {
					uni.uploadFile({
						url: BASE_URL + '/api/users/ups', //仅为示例，非真实的接口地址
						filePath: item,
						name: 'file',
						success: (res) => {
							uni.hideLoading()
							// let obj = {
							// 	img1: JSON.parse(res.data).data.fullurl,
							// 	img2: JSON.parse(res.data).data.url
							// }
							// arr.push(obj)
							// if (arr.length == imgarr.length) {
							// 	resolve(arr)
							// }
							let obj=JSON.parse(res.data).data;
							resolve(obj);
						},
						fail: () => {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					});
				})

			}
		});

	})
}

/**
 * 	arr-- 最终结果   [{img1:'全路径',img2:'半路径'},{img1:'全路径',img2:'半路径'}]
 */
export let upvideo = () => {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			count: 1,
			success: function(res) {
				uni.showLoading({
					title: '上传中'
				})
				let video = res.tempFilePath
				uni.uploadFile({
					url: baseUrl + '/api/common/uploadali', //仅为示例，非真实的接口地址
					filePath: video,
					name: 'file',
					success: (res) => {
						uni.hideLoading()
						console.log('ffffffffffff', res.data);
						if (JSON.parse(res.data).code == '4001') {
							uni.showToast({
								title: JSON.parse(res.data).msg,
								icon: 'none'
							})
						} else {
							let obj = {
								video1: JSON.parse(res.data).data.fullurl,
								video2: JSON.parse(res.data).data.url
							}
							resolve(obj)
						}
					},
					fail: () => {
						uni.showToast({
							title: '上传失败',
							icon: 'none'
						})
					}
				});


			}
		});

	})
}

export let uploadimg = () => {

	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1,
			sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
			success: function(res) {
				uni.showLoading({
					title: '上传中'
				})
				let img = res.tempFilePaths
				console.log(img[0])
				console.log(img)
					uni.uploadFile({
						url: BASE_URL + 'api/users/ups', //仅为示例，非真实的接口地址
						filePath: img[0],
						name: 'file',
						header: {
						     'content-type': 'multipart/form-data'
						},
						success: (res) => {
							uni.hideLoading()
							// let obj = {
							// 	img1: JSON.parse(res.data).data.fullurl,
							// 	img2: JSON.parse(res.data).data.url
							// }
							// arr.push(obj)
							// if (arr.length == imgarr.length) {
							// 	resolve(arr)
							// }
							let obj=JSON.parse(res.data).data;
							resolve(obj);
						},
						fail: (e) => {
							console.log(e)
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					});
				

			}
		});

	})
}


